package com.hzw.saas.service.group.service;

import java.util.Date;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzw.saas.api.group.IGroupMemberReviewService;
import com.hzw.saas.api.group.enums.GroupMemberStatus;
import com.hzw.saas.service.group.mapper.GroupMemberReviewMapper;
import com.hzw.saas.service.group.model.GroupMemberReview;
import cn.hutool.core.date.DateUtil;
import org.springframework.stereotype.Service;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 群组成员审核表 服务实现类
 * </p>
 *
 * @author sonam
 * @since 2020-12-23
 */
@Slf4j
@AllArgsConstructor
@Service("groupMemberReviewServiceImpl")
public class GroupMemberReviewServiceImpl extends ServiceImpl<GroupMemberReviewMapper, GroupMemberReview> implements IGroupMemberReviewService, IService<GroupMemberReview> {

    @Override
    public GroupMemberStatus getGroupMemberStatus(String userId, String groupId, int type) {
        GroupMemberReview groupMemberReview = this.getOne(Wrappers.<GroupMemberReview>lambdaQuery()
            .eq(GroupMemberReview::getUserId, userId)
            .eq(GroupMemberReview::getGroupId, groupId)
            .eq(GroupMemberReview::getType, type));
        if (groupMemberReview == null) {
            return GroupMemberStatus.NO_EXIST;
        }

        Date expireDate = groupMemberReview.getExpireDate();
        if (expireDate == null) {
            // 这条记录是永久的
            return GroupMemberStatus.NORMAL;
        }

        if (expireDate.getTime() > DateUtil.date().getTime()) {
            // 没有过期
            return GroupMemberStatus.NORMAL;
        }

        return GroupMemberStatus.INVALID;
    }

    @Override
    public boolean deleteGroupMemberReview(String userId, String groupId, int type) {
        return this.remove(Wrappers.<GroupMemberReview>lambdaQuery()
            .eq(GroupMemberReview::getUserId, userId)
            .eq(GroupMemberReview::getGroupId, groupId)
            .eq(GroupMemberReview::getType, type));
    }
}
